/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 15, 2013  1:03:21 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.database.dao.promis.impl
 * File Name : CustomerCreditNoteDaoImpl.java
 * TODO
 */
package com.tis.database.dao.promis.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import promis.utility.CommonService;

import com.tis.database.dao.impl.AbstractDaoImpl;
import com.tis.database.dao.promis.CustomerCreditNoteDao;
import com.tis.database.entitys.promis.CustomerCreditNote;
import com.tis.database.entitys.promis.CustomerCreditNotePK;
import com.tis.database.entitys.promis.GlPostingDetail;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 15, 2013 : 1:03:21 PM 
 * CreateBy=Chatree Siangklom
 * 
 * CustomerCreditNoteDaoImpl
 * TODO
 */
@Repository
@SuppressWarnings("unchecked")
public class CustomerCreditNoteDaoImpl extends AbstractDaoImpl<CustomerCreditNote, CustomerCreditNotePK> implements CustomerCreditNoteDao {

	private static final Logger logging = LoggerFactory.getLogger(CustomerCreditNoteDaoImpl.class);
	
	/**
	 * Date =Oct 15, 2013 : 1:03:36 PM 
	 * CreateBy=Chatree Siangklom
	 * CustomerCreditNoteDaoImpl
	 * TODO
	 * @param entityClass
	 */
	protected CustomerCreditNoteDaoImpl() {
		super(CustomerCreditNote.class);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#findById(java.io.Serializable)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public CustomerCreditNote findById(CustomerCreditNotePK id) {
		return super.findById(id);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#saveOrUpdate(java.lang.Object)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(CustomerCreditNote e) {
		super.saveOrUpdate(e);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#saveOrUpdate(java.util.List)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(List<CustomerCreditNote> e) {
		super.saveOrUpdate(e);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#findByCriteria(org.hibernate.criterion.Criterion)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<CustomerCreditNote> findByCriteria(Criterion criterion) {
		return super.findByCriteria(criterion);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#findByAll(int)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<CustomerCreditNote> findByAll(int MaxResults) {
		return super.findByAll(MaxResults);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#findHQLByQuery(java.lang.String, java.util.HashMap)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<CustomerCreditNote> findHQLByQuery(String hql, HashMap<String, String> parameter) {
		return super.findHQLByQuery(hql, parameter);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#SaveOrUpdateHQLByQuery(java.lang.String, java.util.HashMap)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void SaveOrUpdateHQLByQuery(String hql, HashMap<String, String> parameter) {
		super.SaveOrUpdateHQLByQuery(hql, parameter);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#findNameQuery(java.lang.String, java.util.HashMap)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<CustomerCreditNote> findNameQuery(String queryName, HashMap<String, String> parameter) {

		return super.findNameQuery(queryName, parameter);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#SaveOrUpdateQueryName(java.lang.String, java.util.HashMap)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void SaveOrUpdateQueryName(String queryName, HashMap<String, String> parameter) {
		super.SaveOrUpdateQueryName(queryName, parameter);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#NextSequence(java.lang.String)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public BigDecimal NextSequence(String name) throws HibernateException, Exception {

		return super.NextSequence(name);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.AbstractDao#NextInstanceNo()
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public BigDecimal NextInstanceNo() throws HibernateException, Exception {
		return super.NextInstanceNo();
	}
	
	/**
	 * (Javadoc)
	 * @see com.tis.database.dao.promis.CustomerCreditNoteDao#FindByIs(java.lang.String, java.lang.String, java.lang.String, int)
	 * Date =Oct 30, 2013 : 11:26:34 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<CustomerCreditNote> FindByIs(String status, String syncIsstaging, String syncIssync, int maxResult) throws Exception {
		try{
			Session session = getCurrentSession();		
			Query q = session.getNamedQuery(CustomerCreditNote.findByIs);
			
			String query = q.getQueryString();
			if(!CommonService.IsNull(status))
				query += " And upper(c.status) IN("+ status +") ";
			
			q = session.createQuery(query);			
			q.setString("syncIsstaging", syncIsstaging);
			q.setString("syncIssync", syncIssync);
			
			logging.info("status=" + status + "//syncIsstaging=" + syncIsstaging + "//syncIssync=" + syncIssync);
			
			if(maxResult > 0)
				q.setMaxResults(maxResult);
			
			List lst = q.list();
			logging.info("Size=" + CommonService.getSize(lst));
			lst = Collections.checkedList(q.list(), CustomerCreditNote.class);
			
			if(CommonService.getSize(lst) > 0 )
				return lst;
			else 
				return null;
			
		}catch(Exception e){
			e.printStackTrace();	
		}
		return null;
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.promis.CustomerCreditNoteDao#UpdatedBack(com.tis.database.entitys.promis.GlPostingDetail)
	 * Date =Oct 15, 2013 : 1:03:21 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void UpdatedBack(GlPostingDetail glPostingDetail) throws HibernateException, Exception {
		try{
			
			if(glPostingDetail != null){
				
				CustomerCreditNotePK id = new CustomerCreditNotePK();
				id.setCompany(glPostingDetail.getId().getCompany());
				id.setCreditNoteNo(glPostingDetail.getId().getVoucherNumber());
				id.setCreditNoteSeries(glPostingDetail.getId().getVoucherSeries());
				id.setCreditNoteType(glPostingDetail.getId().getVoucherType());
				
				CustomerCreditNote obj = findById(id);
				if(obj != null){
					obj.setSyncExternalRefKey(glPostingDetail.getSyncExternalRefKey());
					obj.setSyncExternalRefNo(glPostingDetail.getSyncExternalRefNo());
					obj.setSyncUpdatedby(glPostingDetail.getSyncUpdatedby());
					obj.setSyncUpdatedon(glPostingDetail.getSyncUpdatedon());
					obj.setSyncIssync(glPostingDetail.getSyncIssync());
					obj.setSyncIsstaging(glPostingDetail.getSyncIsstaging());
					
					saveOrUpdate(obj);
				}
			}			
			
		}catch(Exception e){
			logging.error(e.getMessage());
			e.printStackTrace();
			throw e;
		}
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#getCurrentDatabaseDate()
	 * Date =Oct 17, 2013 : 5:59:11 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public Date getCurrentDatabaseDate() throws HibernateException, Exception {
		return super.getCurrentDatabaseDate();
	}
}
